@echo off
echo ========================================
echo 编译所有微服务
echo ========================================

echo 检查Maven是否安装...
mvn -version >nul 2>&1
if errorlevel 1 (
    echo 错误: 未找到Maven，请先安装Maven
    pause
    exit /b 1
)

echo 开始编译微服务...

REM 编译shared-components
echo.
echo [1/5] 编译 shared-components...
cd shared-components
mvn clean install -DskipTests -B
if errorlevel 1 (
    echo 错误: shared-components 编译失败
    cd ..
    pause
    exit /b 1
)
cd ..
echo ✅ shared-components 编译成功

REM 编译用户管理服务
echo.
echo [2/5] 编译 user-management-service...
cd user-management-service
mvn clean package -DskipTests -B
if errorlevel 1 (
    echo 错误: user-management-service 编译失败
    cd ..
    pause
    exit /b 1
)
cd ..
echo ✅ user-management-service 编译成功

REM 编译课程管理服务
echo.
echo [3/5] 编译 course-management-service...
cd course-management-service
mvn clean package -DskipTests -B
if errorlevel 1 (
    echo 错误: course-management-service 编译失败
    cd ..
    pause
    exit /b 1
)
cd ..
echo ✅ course-management-service 编译成功

REM 编译练习管理服务
echo.
echo [4/5] 编译 exercise-management-service...
cd exercise-management-service
mvn clean package -DskipTests -B
if errorlevel 1 (
    echo 错误: exercise-management-service 编译失败
    cd ..
    pause
    exit /b 1
)
cd ..
echo ✅ exercise-management-service 编译成功

REM 编译学习进度服务
echo.
echo [5/5] 编译 learning-progress-service...
cd learning-progress-service
mvn clean package -DskipTests -B
if errorlevel 1 (
    echo 错误: learning-progress-service 编译失败
    cd ..
    pause
    exit /b 1
)
cd ..
echo ✅ learning-progress-service 编译成功

REM 编译API网关
echo.
echo [6/6] 编译 api-gateway...
cd api-gateway
mvn clean package -DskipTests -B
if errorlevel 1 (
    echo 错误: api-gateway 编译失败
    cd ..
    pause
    exit /b 1
)
cd ..
echo ✅ api-gateway 编译成功

echo.
echo ========================================
echo 所有微服务编译完成！
echo ========================================

echo 检查生成的JAR文件...
echo.
for %%d in (user-management-service course-management-service exercise-management-service learning-progress-service api-gateway) do (
    if exist "%%d\target\*.jar" (
        echo ✅ %%d: 找到JAR文件
    ) else (
        echo ❌ %%d: 未找到JAR文件
    )
)

echo.
echo 现在可以运行 docker-compose up -d --build 来构建和启动服务
pause
